package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

//使用IOC的注解告诉SPring来帮我们管理当前对象
@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired//自动注入
    private EmployeeMapper mapper;

    @Override
    public void save(Employee emp, Long[] ids) {

        //保存员工
        mapper.save(emp);

        //保存关系
        if (ids != null) {
            for (Long id : ids) {
                mapper.saveEmpAndROleRelation(emp.getId(), id);
            }
        }
    }

    @Override
    public void delete(Long id) {
        mapper.delete(id);
    }

    @Override
    public void update(Employee e, Long[] ids) {

        //保存员工
        mapper.update(e);

        //删除旧关系
        mapper.deleteEmpRoleRelation(e.getId());
        //保存新关系
        //保存关系
        if (ids != null) {
            for (Long id : ids) {
                mapper.saveEmpAndROleRelation(e.getId(), id);
            }
        }
    }

    @Override
    public Employee get(Long id) {
        return mapper.get(id);
    }

    @Override
    public List<Employee> listAll() {
        return mapper.listAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        //封装用户需要的数据
        int count = mapper.selectForCount(qo);
        List<Employee> list = mapper.selectForList(qo);

        //将用户需要的所有的数据封装起来
        return new PageResult(list, count, qo.getCurrentPage(), qo.getPageSize());
    }

    public Employee login(String name, String password) {
        //调用mapper中的方法执行查询
        Employee e = mapper.selectByUsernameAndPassword(name, password);
        if (e == null) {
            throw new RuntimeException("亲，您的账号或者密码错误");
        }
        return e;
    }

    public void batchDelete(Long[] ids) {
        mapper.batchDelete(ids);
       /* for (Long id : ids) {
            mapper.delete(id);
        }*/
    }

    public HSSFWorkbook exportXls() {
//        将需要导出的数据查询到
        List<Employee> employees = mapper.listAll();
//
//                使用POI工具将数据写到Excel文件中
//        1.创建工作簿（WorkBook）
        HSSFWorkbook workbook = new HSSFWorkbook();
//        2.创建纸张(Sheet)
        HSSFSheet sheet = workbook.createSheet("员工信息");

        //设置表头
        HSSFRow row = sheet.createRow(0);
        HSSFCell nameCell = row.createCell(0);
        HSSFCell emailCell = row.createCell(1);
        HSSFCell ageCell = row.createCell(2);

        nameCell.setCellValue("名称");
        emailCell.setCellValue("邮箱");
        ageCell.setCellValue("年龄");

        for (int i = 0; i < employees.size(); i++) {
            //        3.创建行（Row）
            HSSFRow newRow = sheet.createRow(i + 1);
//        4.创建单元格（Cell）
            HSSFCell cell1 = newRow.createCell(0);

            HSSFCell cell2 = newRow.createCell(1);
            HSSFCell cell3 = newRow.createCell(2);

//        5.将查询到的数据填充到单元格中
            cell1.setCellValue(employees.get(i).getName());
            cell2.setCellValue(employees.get(i).getEmail());
            cell3.setCellValue(employees.get(i).getAge());
        }
        return workbook;


    }

    public void importXLS(InputStream inputStream) throws IOException {
        //将文件的输入流转换成工作簿
        HSSFWorkbook book = new HSSFWorkbook(inputStream);
        //获取纸张（Sheet）
        HSSFSheet sheet = book.getSheetAt(0);
        //获取行（Row）
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 0; i < lastRowNum; i++) {
            HSSFRow row = sheet.getRow(i + 1);
            //获取单元格（Cell）
            HSSFCell cell1 = row.getCell(0);
            HSSFCell cell2 = row.getCell(1);
            HSSFCell cell3 = row.getCell(2);
            //获取单元格中的数据（value）
            String name = cell1.getStringCellValue();
            String email = cell2.getStringCellValue();
            Double age = cell3.getNumericCellValue();

            System.out.println(name +","+email+","+age);
            //将数据保存到数据库中
            Employee e = new Employee();
            e.setName(name);
            e.setEmail(email);
            e.setAge(age.intValue());

            e.setPassword("666");
            e.setAdmin(false);
            mapper.save(e);
        }

    }


}
